<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 支付宝异步接收控制器
 * @package  onlinepay
 * @author   allen.qiang
 * @version 1.0
 */

require ('core/shop.php');
quanli_include('module/base/common/quanliOnlinepay.class.php');
quanli_include('module/base/system/sysPayment.class.php');

//实例支付方式类
$sysPayment = new sysPayment();

//实例支付类
$ebsigOnlinepay = new quanliOnlinepay();

//接收返回参数
$ebsigOnlinepay->receiveParameter();

//会员号、支付方式ID、订单类型
list($pcustID, $payTypeID, $billType) = explode('|', $ebsigOnlinepay->getParameter('extra_common_param'));

//订单号
$bill_no = $ebsigOnlinepay->getParameter('out_trade_no');

//查询支付方式参数
$online_parameter_array = $sysPayment->searchOnlinepPrameter( $payTypeID );

//检查签名和支付状态
$sign_str = $ebsigOnlinepay->createSignStr(array('sign', 'subsite', 'sign_type'));
$sign_str .= $online_parameter_array['md5_key'];
if (md5($sign_str) != $ebsigOnlinepay->getParameter('sign') || !in_array($ebsigOnlinepay->getParameter('trade_status'), array('TRADE_FINISHED', 'TRADE_SUCCESS'))) {

	//保存服务端返回数据
    $ebsigOnlinepay->setReceiveParams($bill_no, 1, 2);
	die( 'fail' );
	
}

//保存服务端返回数据
$result_code = $ebsigOnlinepay->setReceiveParams($bill_no, 1, 3);

//$result_code 1：订单在本次支付前未付款 -1：订单在本次支付前已付款
if ($result_code == 1) {
    $arg_array = array(
        'bill_no' => $bill_no,
        'pcustID' => $pcustID,
        'payTypeID' => $payTypeID,
        'billType' => $billType,
        'total_amount' => $ebsigOnlinepay->getParameter('total_fee'),
        'pay_code' => $ebsigOnlinepay->getParameter('trade_no')
    );
    $ebsigOnlinepay->payAction( $arg_array );
}

echo 'success';


